package pers.archives.loginToken.authentication;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;
import pers.archives.common.ov.common.CommonResponse;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static pers.archives.common.ov.common.ResponseCode.USER_LOGOUT_SUCCESS;

/* 退出登录
 *@program:archives-parent
 *@author: 赵浩浩
 *@Time: 2020/9/26  20:26
 */
@Slf4j
@Component
public class ArchivesLogoutSuccessHandler implements LogoutSuccessHandler {

    @Autowired
    private ConsumerTokenServices consumerTokenServices;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        String token = request.getHeader("Authorization").toString().split("bearer ")[1];
        boolean b = consumerTokenServices.revokeToken(token);
        if (b) {
            log.info("退出成功");
        }
        response.getWriter().write(new CommonResponse<String>(USER_LOGOUT_SUCCESS, "退出成功").toString());
    }
}
